<?php
namespace app\admin\controller;

use think\Db;
class Category extends Common{
	/**
	 * #素材分组#
	 * @return view
	 */
	function index($val='',$type='-1'){
		// $where=['uid'=>$this->user['id']];
		$where=[];
		$params=[
			'val'=>$val,
			'type'=>$type,
		];
		if($val){
		    $where['name']=['name','like',"%$val%"];
		}
		if($type==0||$type==1){
			$where['status']=$type;
		}
		// 获取所有用户信息
		$user = Db::name('member')->field('id,login_name')->select();
		$list=model($this->table)->readAll($where,true,['order'=>'s_time desc']);
		foreach ($list as $k => $v) {
			$list[$k]['options']=json_decode($v['options'],true);
			foreach ($user as $k2 => $v2) {
				if ($v2['id'] == $v['uid']) {
					$list[$k]['login_name'] = $v2['login_name'];
				}
			}
		}
		$list->appends($params);
		$this->assign('list',$list);
		$this->assign('userl',$user);
		$this->assign('params',$params);
		$this->assign('options',json_encode(config('config.source_options')));
		return view();
	}
	/**
	 * #素材组新增、编辑#
	 * @param  integer $id 素材ID
	 * @return json
	 */
	function add($id=0){
		if(request()->isAjax()||request()->isPost()){
			$uid=input('uid');
			if($id>0){
			    $has=model($this->table)->read(['id'=>$id]);
			    //该项不存在或不属于该用户 做添加操作
			    if(empty($has)){
			        $id=0;
			    }
			}
	        // 获取当前用户id
	        $uids = $this->user['id'];
	        // 获取超级管理员的用户id
	        $suid = config('auth.super_id');
	        // var_dump($suid);
	        // 获取当前素材关联的用户id
	        // 判断当前用户是否是超级管理员
	        if ($uid == $suid && $uids != $suid) {
	            // 修改的是超级管理员的信息，单该用户不是管理员
	            $this->error('权限不足');
	        }

			$data=[
				'id'=>$id,
				'uid'=>$uid,
				'name'=>input('name/s',''),
				'status'=>input('status/d',1),
				'remark'=>input('remark/s',''),
				'options'=>input('options/a',[]),
			];
			$validate=$this->validate($data,[
				'name|分组名称'=>'require',
				'options|分组属性'=>'require',
			]);
			if(true!==$validate){
				$this->error($validate);
			}
			$operation=false;
			$data['options']=json_encode($data['options']);
			if($data['id']>0){
				$operation=model($this->table)->edit($data);
			}else{
				// $data['uid']=$uid;
				$data['s_time']=time();
				$operation=model($this->table)->add($data);
			}
			if(empty($operation)){
                $this->error(lang('Failed operation'));
            }
            $this->success(lang('Successful operation'));
		}
	}
	/**
	 * #素材组禁用、启用#
	 * @param  integer $id 素材组ID
	 * @return json
	 */
	function close($id=0){
	    if(request()->isAjax()||request()->isPost()){
	        $ids=input('ids/a',[]); // 多项
	        if($id<=0&&empty($ids)){
	            $this->error(lang('Data does not exist'));
	        }
	        // 获取当前用户id
	        $uid = $this->user['id'];
	        // 获取超级管理员的用户id
	        $suid = config('auth.super_id');
	        // var_dump($suid);
	        // $member_id=$this->user['id'];
	        // $where=" `uid`={$member_id} ";
	        $where="";
	        if(!empty($ids)){
	            $ids=implode(',', $ids);
	            $where.="`id` in ({$ids}) ";
	        }else{
	            $where.="`id`={$id} ";
	        }

	        // 获取当前素材关联的用户id
			$cates = Db::name('category')->where($where)->field('uid')->select();
			$cates = json_decode($cates,true);
			// var_dump(in_array(['uid'=>$suid], $cates, TRUE));die;
			// if (in_array(['uid'=>$suid], $people, TRUE)) {
			// 	# code...
			// }
	        // 判断当前用户是否是超级管理员
	        if (in_array(['uid'=>$suid], $cates, TRUE) && $uid != $suid) {
	            // 修改的是超级管理员的信息，单该用户不是管理员
	            $this->error('权限不足');
	        }

	        $rs=model($this->table)->close($where,'status',true);
	        if($rs){
	            $this->success(lang('Successful operation'));
	            die;
	        }
	        $this->error(lang('Failed operation'));
	    }
	}
	/**
	 * #素材组删除#
	 * @param  integer $id 素材组ID
	 * @return [type]      [description]
	 */
	function del($id=0){
	    if(request()->isAjax()||request()->isPost()){
	        $ids=input('ids/a',[]); // 多项删除
	        if($id<=0&&empty($ids)){
	            $this->error(lang('Data does not exist'));
	        }
	        // $member_id=$this->user['id'];
	        // $where=['id'=>$id,'uid'=>$member_id];
	        $where=['id'=>$id];

	        // 获取当前用户id
	        $uid = $this->user['id'];
	        // 获取超级管理员的用户id
	        $suid = config('auth.super_id');
	        // var_dump($suid);

	        if(!empty($ids)){
	            $where=[
	                // 'uid'=>$member_id,
	                'id'=>['id','in',$ids]
	            ];
	        }
	        // 获取当前素材关联的用户id
			$cates = Db::name('category')->where($where)->field('uid')->select();
			$cates = json_decode($cates,true);
			// var_dump(in_array(['uid'=>$suid], $cates, TRUE));die;
			// if (in_array(['uid'=>$suid], $people, TRUE)) {
			// 	# code...
			// }
	        // 判断当前用户是否是超级管理员
	        if (in_array(['uid'=>$suid], $cates, TRUE) && $uid != $suid) {
	            // 修改的是超级管理员的信息，单该用户不是管理员
	            $this->error('权限不足');
	        }
	        
	        $del=model($this->table)->del($where);
	        if($del){
	            // 删除其它关联 
	            
	            $this->success(lang('Successful operation'));
	            die;
	        }
	        $this->error(lang('Failed operation'));
	    }
	}
	/**
	 * 获取分组信息
	 * @param  integer $id     [description]
	 * @param  integer $status [description]
	 * @return [type]          [description]
	 */
	function getCates($id=0,$status=1){
		$where=[
			'status'=>$status,
			'uid'=>$this->user['id'],
		];
		if($id>0){
			$where['id']=$id;
		}
		$cate=model('category')->readAll($where);
		if(request()->isAjax()||request()->isPost()){
			$this->success('','',$cate);
		}else{
			return $cate;
		}
	}
}